package com.bw.controller;

import com.alibaba.fastjson2.JSON;
import com.bw.domain.Role;
import com.bw.domain.TbMenu;
import com.bw.service.RoleService;
import com.bw.service.UserService;
import com.bw.utils.MenuUtil;
import com.bw.utils.R;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author :Beyond🎸🎸
 * @date : 2025-03-03 19:23:12
 * @description Rock and Roll
 **/
@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;
    /**
    * @Author: Koma🤘🤘
    * @Description: 跳转到角色列表
    * @Param: 
    * @return: 
    * @Date: 2025/3/3 19:24
    */
    @RequestMapping("tolist")
    public String tolist(){
        return "role/list";
    }
    /**
    * @Author: Koma🤘🤘
    * @Description: 跳转添加角色
    * @Param:
    * @return:
    * @Date: 2025/3/3 19:23
    */
    @RequestMapping("toadd")
        public String toadd(Model mode){
//        获取所有菜单
        List<TbMenu> allTbMenuList = userService.listMenus();
//        转成树形结构
        List<TbMenu> treeAllMenuList = MenuUtil.treeMenus(allTbMenuList,null);
//        把数据存储到MODEL域里
        mode.addAttribute("treeAllMenuList", JSON.toJSON(treeAllMenuList));
        return "role/add";
    }

    @ResponseBody
    @RequestMapping("add")
    public R add(Role role,String menuIds){
        Boolean i = roleService.add(role,menuIds);
        return i?R.OK():R.ERROR(500,"保存失败");
    }
}
